package com.gsmr.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.gsmr.entity.FrontProcessorInfo;
import com.gsmr.entity.FrontProcessorInfoAndFrontProcessorState;
import com.gsmr.entity.FrontProcessorState;
import com.gsmr.entity.PageBean;
import com.gsmr.service.FrontProcessorInfoService;
import com.gsmr.service.FrontProcessorStateService;
import com.gsmr.service.SysOperationRecordService;
import com.gsmr.util.PropertityUtil;
import com.gsmr.util.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author 作者 : 黄志新
 * @version 创建时间：2018年5月24日 上午10:46:55 类说明：
 */
@Controller
@RequestMapping("/frontProcessorState")
public class FrontProcessorStateController {

	@Resource
	private FrontProcessorStateService frontProcessorStateService;

	@Resource
	private SysOperationRecordService sysOperationRecordService;

	@Resource
	private FrontProcessorInfoService frontProcessorInfoService;

	/**
	 * 查找前置机状态信息
	 */
	@RequestMapping("/statelist")
	public String listFrontProcessorInfoAndFrontProcessorState(@RequestParam(value = "page", required = false) String page, @RequestParam(value = "rows", required = false) String rows, @RequestParam(value = "frontProcessorId", required = false) String frontProcessorId, FrontProcessorInfoAndFrontProcessorState frontProcessorInfoAndFrontProcessorState, HttpServletResponse response) throws Exception {

		PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		map.put("database_name", PropertityUtil.NETWORK_MANAGEMENT_DATABASE_NAME);
		map.put("frontProcessorId", frontProcessorId);

		List<FrontProcessorInfo> frontprocessorInfoList = frontProcessorInfoService.find(map);
		List<FrontProcessorInfoAndFrontProcessorState> frontProcessorInfoAndFrontProcessorStateList = new ArrayList<FrontProcessorInfoAndFrontProcessorState>();

		for (FrontProcessorInfo frontprocessorInfo : frontprocessorInfoList) {

			FrontProcessorInfoAndFrontProcessorState fpiafps = new FrontProcessorInfoAndFrontProcessorState();
			fpiafps.setId(frontprocessorInfo.getId());
			fpiafps.setFrontProcessorId(frontprocessorInfo.getFrontProcessorId());
			fpiafps.setIpAddress(frontprocessorInfo.getIpAddress());
			fpiafps.setSiteName(frontprocessorInfo.getSiteName());
			fpiafps.setBcchInfo(frontprocessorInfo.getBcchInfo());
			fpiafps.setHardwareVersion(frontprocessorInfo.getHardwareVersion());
			fpiafps.setSoftwareVersion(frontprocessorInfo.getSoftwareVersion());
			fpiafps.setMonitoringFrequency(frontprocessorInfo.getMonitoringFrequency());
			fpiafps.setReportInterval(frontprocessorInfo.getReportInterval());
			fpiafps.setSpectrumSendSwitch(frontprocessorInfo.getSpectrumSendSwitch());
			fpiafps.setSpectrumReportedIpAddress(frontprocessorInfo.getSpectrumReportedIpAddress());
			fpiafps.setSignalSendSwitch(frontprocessorInfo.getSignalSendSwitch());
			fpiafps.setSignalReportedIpAddress(frontprocessorInfo.getSignalReportedIpAddress());
			fpiafps.setTchSendSwitch(frontprocessorInfo.getTchSendSwitch());
			fpiafps.setTchReportedIpAddress(frontprocessorInfo.getTchReportedIpAddress());
			fpiafps.setIconPositionX(frontprocessorInfo.getIconPositionX());
			fpiafps.setIconPositionY(frontprocessorInfo.getIconPositionY());
			fpiafps.setCreateTime(frontprocessorInfo.getCreateTime());

			map.put("frontProcessorId", frontprocessorInfo.getFrontProcessorId());
			List<FrontProcessorState> frontProcessorStateList = frontProcessorStateService.find(map);
			if (frontProcessorStateList.size() > 0) {
				fpiafps.setCpuUsageRate(frontProcessorStateList.get(0).getCpuUsageRate());
				fpiafps.setHarddiskUsageRate(frontProcessorStateList.get(0).getHarddiskUsageRate());
				fpiafps.setMemoryUsageRate(frontProcessorStateList.get(0).getMemoryUsageRate());
				fpiafps.setIsNormal(frontProcessorStateList.get(0).getIsNormal());
				fpiafps.setAlarmInfo("故障状态码：" + frontProcessorStateList.get(0).getAlarmInfo() + ",故障时间：" + frontProcessorStateList.get(0).getCreateTime());
				fpiafps.setPortFlowValue(frontProcessorStateList.get(0).getPortFlowValue());
			}
			frontProcessorInfoAndFrontProcessorStateList.add(fpiafps);
		}
		int total = frontProcessorInfoAndFrontProcessorStateList.size();//frontProcessorInfoService.getTotal(map);
		JSONObject result = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(frontProcessorInfoAndFrontProcessorStateList);
		result.put("rows", jsonArray);
		System.out.println("data: " + jsonArray);
		result.put("total", total);
		ResponseUtil.write(response, result);
		return null;
	}

	/**
	 * 分页条件查询FrontProcessorState
	 */
	@RequestMapping("/list")
	public String list(@RequestParam(value = "page", required = false) String page, @RequestParam(value = "rows", required = false) String rows, FrontProcessorState frontProcessorState, HttpServletResponse response) throws Exception {

		PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		map.put("database_name", PropertityUtil.NETWORK_MANAGEMENT_DATABASE_NAME);
		List<FrontProcessorState> frontProcessorStateList = frontProcessorStateService.find(map);
		Long total = frontProcessorStateService.getTotal(map);
		JSONObject result = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(frontProcessorStateList);
		result.put("rows", jsonArray);
		System.out.println(jsonArray);
		result.put("total", total);
		ResponseUtil.write(response, result);
		return null;
	}

	/**
	 * 查询FrontProcessorState的实时CPU使用率、内存使用率、硬盘使用率
	 * 
	 * @param frontProcessorId
	 * @param startTime
	 * @param endTime
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/listRealtimeUsageRate")
	public String listRealtimeUsageRate(
		@RequestParam(value = "frontProcessorId", required = true) String frontProcessorId,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();

		/*
		 * //先获取前置机状态和UDP指令 map.put("database_name",
		 * PropertityUtil.NETWORK_MANAGEMENT_DATABASE_NAME);
		 * ArrayList<Map<String, String>> sysoperationrecordList = new
		 * ArrayList<Map<String, String>>(); LinkedHashMap hm = new
		 * LinkedHashMap(); hm.put("operateTime", System.currentTimeMillis());
		 * hm.put("userId", 1); hm.put("operationType", 10); JSONObject
		 * jsonObject = new JSONObject(); jsonObject.put("frontProcessorId",
		 * Integer.parseInt(frontProcessorId)); hm.put("newValue",
		 * jsonObject.toString()); hm.put("isImplemented", 0);
		 * sysoperationrecordList.add(hm); map.put("list",
		 * sysoperationrecordList); sysOperationRecordService.add(map);
		 * 
		 * //然后根据通信协议要求，先发送一条其他指令让前置机保存服务器IP hm.put("operationType", 10);
		 * sysOperationRecordService.add(map);
		 */

		map.clear();
		map.put("database_name", PropertityUtil.NETWORK_MANAGEMENT_DATABASE_NAME);
		// 再查询查询本地数据库
		map.put("start", 0);
		map.put("size", 1);
		map.put("frontProcessorId", frontProcessorId);
		List<FrontProcessorState> tmpList = frontProcessorStateService.find(map);
		JSONObject result = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(tmpList);
		result.put("rows", jsonArray);
		ResponseUtil.write(response, result);
		return null;
	}

	/**
	 * 查询FrontProcessorState的历史CPU使用率、内存使用率、硬盘使用率
	 * 
	 * @param frontProcessorId
	 * @param startTime
	 * @param endTime
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/listHistoryUsageRate")
	public String listHistoryUsageRate(@RequestParam(value = "startTime", required = false) String startTime, @RequestParam(value = "endTime", required = false) String endTime, @RequestParam(value = "frontProcessorId", required = true) String frontProcessorId,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("database_name", PropertityUtil.NETWORK_MANAGEMENT_DATABASE_NAME);
		if (startTime != null && !startTime.equals("") && !startTime.equals("NaN")) {
			map.put("startTime", startTime);
		}
		if (endTime != null && !endTime.equals("") && !endTime.equals("NaN")) {
			map.put("endTime", endTime);
		}
		map.put("frontProcessorId", frontProcessorId);
		List<FrontProcessorState> tmpList = frontProcessorStateService.find(map);
		JSONObject result = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(tmpList);
		result.put("rows", jsonArray);
		ResponseUtil.write(response, result);
		return null;
	}

}
